<!--
  * 全景图地点
  *
  * @Author:    骆伟林
  * @Date:      2025-10-23 14:52:49
  * @Copyright  软件需求工程第八组
-->
<template>
  <a-modal :title="form.addressId ? '编辑' : '添加'" :width="800" :open="visibleFlag" @cancel="onClose" :maskClosable="false" :destroyOnClose="true">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" class="smart-query-form">
      <a-row class="smart-query-form-row">
        <a-col :span="8">
          <a-form-item label="名称" class="smart-query-form-item">
            <a-input style="width: 100%" v-model:value="form.addressName" placeholder="请输入地点名称" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="经度" class="smart-query-form-item">
            <a-input-number style="width: 100%" v-model:value="form.longitude" placeholder="经度" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="维度" class="smart-query-form-item">
            <a-input-number style="width: 100%" v-model:value="form.latitude" placeholder="维度" disabled />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <a-row>
      <Map :longitude="form.longitude" :latitude="form.latitude" :latAndLon="form.latAndLon" @getArea="getArea" />
    </a-row>
    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick, onMounted, onUnmounted, computed } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { addressApi } from '/@/api/business/address/address-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import Map from '../components/address-form-map.vue';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  function getArea(lng, lat) {
    form.latitude = lat;
    form.longitude = lng;
  }

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    addressId: undefined, //地点id
    addressName: undefined, //地点名称
    longitude: undefined, //经度
    latitude: undefined, //维度
    latAndLon: [undefined, undefined],
  };

  let form = reactive({ ...formDefault });

  const rules = {
    addressName: [{ required: true, message: '地点名称 必填' }],
    longitude: [{ required: true, message: '经度 必填' }],
    latitude: [{ required: true, message: '维度 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.addressId) {
        await addressApi.update(form);
      } else {
        await addressApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
<style scoped>
  #container {
    width: 100%;
    height: 70vh;
  }
  .input-card {
    margin-top: 10px;
  }
  .controlShow-button {
    position: absolute;
    top: 10%;
    right: 5%;
    z-index: 5;
  }
</style>
